package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Supplier;
import com.ruoyi.purchase.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * SupplierController
 * 
 * @author kevinness
 * @date 2025-02-02
 */
@RestController
@RequestMapping("/Supplier")
public class SupplierController extends BaseController
{
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询Supplier列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier)
    {
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        return getDataTable(list);
    }

    /**
     * 导出Supplier列表
     */
    @Log(title = "Supplier", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supplier supplier)
    {
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        util.exportExcel(response, list, "Supplier数据");
    }

    /**
     * 获取Supplier详细信息
     */
    @GetMapping(value = "/{supplierCode}")
    public AjaxResult getInfo(@PathVariable("supplierCode") String supplierCode)
    {
        return success(supplierService.selectSupplierBySupplierCode(supplierCode));
    }

    /**
     * 根据用户名获取供货商详细信息
     */
    @PostMapping("/getSupplierByUsername")
    public AjaxResult getSupplierByUsername(@RequestBody String username){
        return success(supplierService.selectSupplierInfoByUsername(username));
    }

    /**
     * 根据供货商名称获取供货商编码
     */
    @PostMapping("/getSupplierCodeByCompanyName")
    public AjaxResult getSupplierCodeByCompanyName(@RequestBody String companyName){
        return success(supplierService.selectSupplierCodeByCompanyName(companyName));
    }

    /**
     * 新增Supplier
     */
    @Log(title = "Supplier", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supplier supplier)
    {
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改Supplier
     */
    @Log(title = "Supplier", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier)
    {
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除Supplier
     */
    @Log(title = "Supplier", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierCodes}")
    public AjaxResult remove(@PathVariable String[] supplierCodes)
    {
        return toAjax(supplierService.deleteSupplierBySupplierCodes(supplierCodes));
    }
}
